package com.example.demo1.controller;

import com.example.demo1.common.WrapMapper;
import com.example.demo1.common.Wrapper;
import com.example.demo1.entity.User;
import com.example.demo1.service.UserService;
import com.example.demo1.util.JWTUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(value = "/first")
public class UserController {
    @Autowired
    UserService userService;

    // 1、注册
    @PostMapping("/register")
    public Wrapper register(@Validated @RequestBody User user){
        return userService.register(user);
    }

    // 2、登录 - 修改返回Token
    @PostMapping("/login")
    public Wrapper login(@RequestBody User user, HttpServletRequest request) {
        Wrapper loginResult = userService.login(user.getStuName(), user.getStuId());

        // 如果登录成功，生成Token
        if (loginResult.getCode() == 200) {
            String token = JWTUtil.generateToken(user.getStuName(), "STUDENT");
            // 将token加入返回结果
            loginResult.setToken(token);
        }

        request.getSession().setAttribute("userInfo", user);
        return loginResult;
    }

    // 3、查询所有用户信息
    @GetMapping("/getAllUser")
    public Wrapper getAllUser(HttpServletRequest request) {
        // 从request中获取角色信息
        String role = (String) request.getAttribute("role");

        // 权限验证
        if (!"STUDENT".equals(role)) {
            return WrapMapper.error("无权访问");
        }

        User user = (User) request.getSession().getAttribute("userInfo");
        List<User> userList = userService.getAllUser();
        return WrapMapper.ok(userList);
    }

    // 4、修改用户信息
    @PostMapping("/update")
    public Wrapper updateUser(@Validated @RequestBody User user) {
        return userService.updateUser(user);
    }

    // 5、删除用户
    @PostMapping("/delete/{stuId}")
    public Wrapper deleteUser(@PathVariable String stuId) {
        return userService.deleteUser(stuId);
    }

    // 6、查询当前可选课程
    @GetMapping("/{stuId}/getAllCourse")
    public Wrapper getAllCourse(HttpServletRequest request, @PathVariable String stuId) {
        String role = (String) request.getAttribute("role");

        if (!"STUDENT".equals(role)) {
            return WrapMapper.error("无权访问");
        }

        return userService.getAllCourses(stuId);
    }

    // 7、根据课程id查询课程信息
    @GetMapping("/{stuId}/getCourse/{courseId}")
    public Wrapper getCourseInfo(@PathVariable String stuId, @PathVariable String courseId) {
        return userService.getCourseInfo(stuId, courseId);
    }


    // 8、用户进行选课
    @PostMapping("/enroll")
    public Wrapper enrollCourse(@RequestParam String stuId, @RequestParam String courseId) {
        return userService.enrollCourse(stuId, courseId);
    }

    // 9、根据用户id查询已选课程
    @GetMapping("/enrolled")
    public Wrapper EnrolledCourses(@RequestParam String stuId) {
        return userService.EnrolledCourses(stuId);
    }


    // 10、用户退课（删除选课）
    @PostMapping("/{stuId}/course/{courseId}")
    public Wrapper deleteCourses(@PathVariable String stuId, @PathVariable String courseId) {
        String result = userService.dropCourse(stuId, courseId);
        return WrapMapper.ok(result);
    }
}